<template>
  <section>
    <div class="filter-container">
      <!-- <el-select size="mini" v-model="listQuery.typeId" clearable placeholder="全部分类" class="ml10" @change="handleFilter">
        <el-option v-for="item in catList" :key="item.id" :label="item.typeName" :value="item.id">
        </el-option>
      </el-select> -->
      <common-input @keyup.enter.native="handleFilter" class="w200 ml10" placeholder="关键字"
        v-model="listQuery.keyword" />
      <el-button size="mini" class="ml10" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button size="mini" class="ml10" @click="handleCreate" type="primary" icon="el-icon-plus">新增</el-button>
    </div>
    <el-table :data="list" style="width: 100%">
      <el-table-column align="left" label="标题">
        <template slot-scope="scope">

          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="缩略图">
        <template slot-scope="scope">
          <img :src="scope.row.imgUrl" alt="" width="80" height="30">
        </template>
      </el-table-column>
      <el-table-column align="left" label="描述">
        <template slot-scope="scope">
          <span>{{ scope.row.descrition }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="观看次数" prop="hit"></el-table-column>
      <el-table-column align="center" label="排序">
        <template slot-scope="scope">
          <span>{{ scope.row.orderNum }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button type="text">操作<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleUpdate(scope.row)">编辑</el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(scope.row)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="listQuery.pageNum" :page-sizes="[20, 30, 50]" :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false"
      width="500px" @close="dialogClose">
      <el-form :model="form" :rules="rules" ref="form" label-width="80px">
        <el-form-item label="标题" prop="title">
          <common-input type="text" v-model="form.title" placeholder="请输入问题" />
        </el-form-item>
        <el-form-item label="描述" prop="descrition">
          <common-input type="textarea" v-model="form.descrition" placeholder="请输入问题" />
        </el-form-item>
        <el-form-item label="缩略图" prop="imgUrl">
          <pic-sel v-model="form.imgUrl"></pic-sel>
        </el-form-item>
        <el-form-item label="视频文件" prop="videoUrl">{{ form.videoUrl }}
          <el-input size="mini" v-model="form.videoUrl" placeholder="" />

          <el-upload :before-upload="beforePicUpload" class="upload-demo" drag action="" :limit="1" :multiple="false">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">上传100M以内视频，将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
        </el-form-item>
        <el-form-item label="所属分类" prop="typeId">
          <el-select size="mini" v-model="form.typeId">
            <el-option v-for="item in catList" :key="item.id" :label="item.typeName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <common-input v-model="form.orderNum" class="w60" maxlength="3" />
        </el-form-item>
        <el-form-item label="播放次数" prop="hit">
          <common-input v-model="form.hit" class="w60" maxlength="3" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="cancel('form')">取消</el-button>
        <el-button size="mini" v-if="dialogStatus == 'create'" type="primary" @click="create('form')">确定</el-button>
        <el-button size="mini" v-else type="primary" @click="update('form')">确定</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import {

  uploadSubmit
} from '../../api/config';
import {
  isIntegerNotMust
} from '../../utils/validator'
import {
  coursePageList,
  courseDelete,
  courseSave,
  courseType
} from '@/api/setting';
// import {
//     qa_cat_list
// } from '@/api/setting';
export default {
  components: {
    'pic-sel': () => import('../../components/PicSel/index'),
    // 'pic-view': () => import('../../components/PicView/index'),
    // 'pic-list': () => import('../../components/PicSel/list'),
  },
  data() {
    return {
      catList: [],
      list: null,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        keyword: undefined,
        typeId: undefined,

      },
      total: null,
      dialogStatus: '',
      dialogFormVisible: false,
      textMap: {
        update: '编辑教程',
        create: '新增教程'
      },
      form: {
        id: undefined,
        title: undefined,
        imgUrl: undefined,
        videoUrl: undefined,
        orderNum: 0,
        hit: 0,
        typeId: undefined
      },
      rules: {
        title: [
          {
            required: true,
            message: '请输入问题',
            trigger: 'blur'
          }
        ],
        videoUrl: [
          {
            required: true,
            message: '请上传视频',
            trigger: 'blur'
          }
        ],
        // catId: [
        //     {
        //         required: true,
        //         message: '请选择分类',
        //         trigger: 'blur'
        //     }
        // ],
        orderNum: [
          {
            validator: isIntegerNotMust,
            trigger: 'blur'
          }
        ]
      },
    }
  },
  mounted() {
  },
  created() {
    this.getCatList();
    this.getList();
  },

  methods: {
    setValue() {
      console.log(11111111111111)
      this.form.videoUrl = this.form.videoUrl
    },
    beforePicUpload(file) {
      console.log(file)
      const isLt2M = file.size / 1024 / 1024 < 100;

      if (!isLt2M) {
        this.$message.error('上传的文件大小不能超过 100MB!');
        return false
      }
      if (file.type !== "video/mp4") {
        this.$message.error("只能上传mp4视频格式文件")
        // Message({
        //     message: "只能上传apk格式文件",
        //     type: 'error',
        //     duration: 5 * 1000
        // });
        return false
      }

      let param = new FormData();
      param.append('file', file);
      this.loading = this.$loading({
        lock: true,
        text: '上传中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      let that = this
      uploadSubmit(param)
        .then(response => {
          that.loading.close();
          if (response.code == 200) {
            this.$message.success("上传成功");
            that.form.videoUrl = response.data.uri;
            that.form.title = "1"
          }

          // console.log(that.form, response.data.uri)
          // that.setValue()
          // this.uploadForm = response.data;
          // this.uploadForm.isCurrent = true;
          // this.uploadForm.isForce = false;
          // this.step = 2;
        }).catch((e) => {
          that.loading.close();
          console.log(e)
        });

      return false
    },
    getCatList() {
      courseType().then(response => {
        this.catList = response.data.list;
      })
    },
    getList() {
      this.listLoading = true;
      coursePageList(this.listQuery).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      })
    },
    handleCreate() {
      this.$router.push({ path: '/setting/video_create' });
      // this.resetTemp();
      // this.dialogStatus = 'create';
      // this.dialogFormVisible = true;
    },
    resetTemp() {
      this.form = {
        id: undefined,
        question: undefined,
        answer: undefined,
        catId: undefined,
        orderNum: 0,
      };
    },
    create(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          if (!this.form.orderNum) {
            this.form.orderNum = 0;
          }
          courseSave(this.form)
            .then(() => {
              this.dialogFormVisible = false;
              this.getList();
              this.$message.success("问题创建成功");
            })
        } else {
          return false;
        }
      });
    },
    update(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          if (!this.form.orderNum) {
            this.form.orderNum = 0;
          }
          this.dialogFormVisible = false;
          this.form.password = undefined;
          courseSave(this.form).then(() => {
            this.dialogFormVisible = false;
            this.getList();
            this.$message.success("问题更新成功");
          });
        } else {
          return false;
        }
      });
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    handleUpdate(row) {
      // this.form = row;
      // this.dialogFormVisible = true;
      // this.dialogStatus = 'update';
      this.$router.push({
        path: '/setting/video_create',
        query: { row: JSON.stringify(row) }
      });
      // qa_detail(row.id)
      //     .then(response => {
      //         this.form = response.data;
      //         this.dialogFormVisible = true;
      //         this.dialogStatus = 'update';
      //     });
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          courseDelete(row.id)
            .then(() => {
              this.$message.success("问题删除成功");
              const index = this.list.indexOf(row);
              this.list.splice(index, 1);
            });
        }).catch(() => { });
    },
    dialogClose() {
      this.$refs.form.resetFields();
    },
    handleFilter() {
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    }
  }
};
</script>
